package com.isias.system.service.Impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isias.model.El.ElQu;
import com.isias.model.El.ElUserPaper;
import com.isias.model.El.ElUserPaperAnswer;
import com.isias.model.dto.ElUserPaperAnswerDto;
import com.isias.model.dto.ElUserPaperDto;
import com.isias.model.dto.PaperQuPointDto;
import com.isias.model.dto.WorkDetailDto;
import com.isias.model.vo.ElPaperQueryVo;
import com.isias.model.vo.ElQuQueryVo;
import com.isias.model.vo.WorkDetailVo;
import com.isias.system.handler.CreateExamException;
import com.isias.system.mapper.ElPaperMapper;
import com.isias.system.mapper.ElUserPaperAnswerMapper;
import com.isias.system.service.ElUserPaperAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ElUserPaperAnswerServiceImpl extends ServiceImpl<ElUserPaperAnswerMapper, ElUserPaperAnswer> implements ElUserPaperAnswerService {

    @Autowired
    private ElUserPaperAnswerMapper elUserPaperAnswerMapper;

    @Autowired
    private ElPaperMapper elPaperMapper;


    /***
     * 向数据库中插入考生和考试卷子的信息，然后copy一份题目信息到el_user_paper_answer表，用来最终计算成绩
     */
    ElUserPaper elUserPaper = new ElUserPaper();
    @Override
    public String CreatPaperToUser(String userId, String paperId) {
        elUserPaper.setUserId(userId);
        elUserPaper.setPaperId(paperId);
        if (elUserPaperAnswerMapper.CreatPaperToUser(elUserPaper)==0){
            throw new CreateExamException();    //没更新就给我报错！抛出异常！给我rollback！给我死！
        }
        if (elUserPaperAnswerMapper.batchInsertDatas(elUserPaper.getId(),elPaperMapper.SelectQuIdByPaperId(paperId)) == 0) {
            throw new CreateExamException();
        }
//        List<ElUserPaperAnswer> list = new ArrayList<>();   //先创建出来list
//        //根据paperid查询出来题目信息,迭代循环把
//        for (String s : elPaperMapper.SelectQuIdByPaperId(paperId)) {
//            ElUserPaperAnswer elUserPaperAnswer = new ElUserPaperAnswer();  //还tm必须在循环里new对象;gc回收器:啊对对对，就这么new，你看你内存爆不爆炸
//            //设置实体类Quid和elUserpaperid，然后添加到list里
//            elUserPaperAnswer.setElUserPaperId(elUserPaper.getId());
//            elUserPaperAnswer.setQuId(s);
//            list.add(elUserPaperAnswer);
//        }
//        //调用mapper copy题目信息到el_user_paper_answer表
//        if (elUserPaperAnswerMapper.batchInsertData(list) == 0){
//            throw new CreateExamException();        //你也一样！没更新就给我死!
//        }
        return elUserPaper.getId();
    }

    @Override
    public List<ElUserPaperAnswerDto> InitQu(String elUserPaperId) {
        return elUserPaperAnswerMapper.InitQu(elUserPaperId);
    }

    @Override
    public ElUserPaperAnswerDto GetOneQu(String elUserPaperId, String quId) {
        return elUserPaperAnswerMapper.GetOneQu(elUserPaperId,quId);
    }

    @Override
    public Boolean UpdateList(List<ElUserPaperAnswer> list,String paperId,String elUserPaperId) {
        if (elUserPaperAnswerMapper.batchUpdateDatas(list) == 0){
            return false;
        }
        int sum = 0;
        PaperQuPointDto paperQuPointDto = elPaperMapper.SelectQuPoint(paperId); //获取该试卷中各种题目的分数
        for (ElUserPaperAnswer elUserPaperAnswer : list) {
            if ("1".equals(elUserPaperAnswer.getQuType())){ //单选题
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){  //如果正确
                    sum += paperQuPointDto.getRadioScore();
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getRadioScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }else if ("2".equals(elUserPaperAnswer.getQuType())){
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){
                    sum += paperQuPointDto.getMultiScore();
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getMultiScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }else if ("3".equals(elUserPaperAnswer.getQuType())){
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){
                    sum += paperQuPointDto.getJudgeScore();
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getJudgeScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }
        }
        if(elUserPaperAnswerMapper.updateElUserPaper(sum,elUserPaperId)==0){
            return false;
        }

        return true;
    }

    /***
     * 刷新每道题的分数和总分
     */
    @Override
    public void UpdatePoint(String elUserPaperId){
        List<ElUserPaperAnswer> list = elUserPaperAnswerMapper.GetElUserPaperAnswerByElUserPaperID(elUserPaperId);
        String paperId = elUserPaperAnswerMapper.GetPaperIdByElUserPaperID(elUserPaperId);
        PaperQuPointDto paperQuPointDto = elPaperMapper.SelectQuPoint(paperId); //获取该试卷中各种题目的分数
        for (ElUserPaperAnswer elUserPaperAnswer : list) {
            if ("1".equals(elUserPaperAnswer.getQuType())){ //单选题
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){  //如果正确
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getRadioScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }else if ("2".equals(elUserPaperAnswer.getQuType())){
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getMultiScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }else if ("3".equals(elUserPaperAnswer.getQuType())){
                if (elUserPaperAnswer.getAnswer().equals(elUserPaperAnswer.getUanswer())){
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),paperQuPointDto.getJudgeScore());
                }else {
                    elUserPaperAnswerMapper.updateGetPoint(elUserPaperAnswer.getId(),0);
                }
            }
        }
        elUserPaperAnswerMapper.updateElUserPaper(elUserPaperAnswerMapper.GetsumPoint(elUserPaperId),elUserPaperId);

    }

    @Override
    public Boolean updateGetPoint(String elUserPaperId, int point) {
        return elUserPaperAnswerMapper.updateGetPoint(elUserPaperId,point)>0;
    }

    @Override
    public IPage<ElUserPaperDto> Mywork(Page<ElUserPaperDto> pageparam, String id, ElPaperQueryVo elPaperQueryVo) {
        return elUserPaperAnswerMapper.Mywork(pageparam,id,elPaperQueryVo);
    }

    @Override
    public IPage<WorkDetailDto> workDetail(Page<WorkDetailDto> pageparam, String paperId, WorkDetailVo workDetailVo) {
        return elUserPaperAnswerMapper.workDetail(pageparam,paperId,workDetailVo);
    }
}
